<?php
//require_once('core/pglob.php');

class mail_module{

	/**
	 * 发送邮件至邮件队列表中
	 *
	 * @param array $mail_array	邮件内容数组
	 * 例：array('receivers'=>'test','mail_type'=>1,'mail_title'=>'测试用','mail_body'=>'测试内容','creator'=>'system',
	 * 		'schedule_time'=>'2011-07-11 12:03:12','remark'=>'测试','relative_code'=>'123434')
	 */
	public function mail_create($mail_array){
		global $G_PGLOB;
		$mails = new entity('ps_mails');
		$mails->mail_type = $mail_array['mail_type'];
		$mails->receivers = $mail_array['receivers'];
		$mails->mail_title = $mail_array['mail_title'];
		$mails->mail_body = $mail_array['mail_body'];
		$mails->creator = $mail_array['creator'];
		if (isset($mail_array['schedule_time']))
			$mails->schedule_time = $mail_array['schedule_time'];
		if (isset($mail_array['remark']))
			$mails->remark = $mail_array['remark'];
		if (isset($mail_array['relative_code']))
			$mails->relative_code = $mail_array['relative_code'];
					
		$mails->sent_times = 0;
		$mails->sent_ok = 0;
		$G_PGLOB->DBCA->save($mails,'backend');
	}
	/**
	 *  记录错误信息
	 *
	 */
	private static $_currentErrorMessage;
	public function getCurrentErrorMessage(){
        return self::$_currentErrorMessage;
    }

	public function checkEmail($inAddress){   
		return (ereg("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+",$inAddress));   
	} 
	/**
	 * 发送邮件至邮件队列表中
	 *
	 * @param array $mail_array	邮件内容数组
	 * 例：array('receivers'=>'test','mail_type'=>1,'mail_title'=>'测试用','mail_body'=>'测试内容','creator'=>'system',
	 * 		'schedule_time'=>'2011-07-11 12:03:12','remark'=>'测试','relative_code'=>'123434')date('Y-m-d H:i:s')
	 */
	public function syns_mail_create($mail_array, &$dbca){ 
		$mails = new entity('a_mails');
		/*
		$inAddress = self::checkEmail($mail_array['receivers']);
		if(!$inAddress){
			self::$_currentErrorMessage = '请正确填写邮箱地址';
   		    return -1;
		}
		*/
		$mails->mail_type = $mail_array['mail_type'];
		$mails->receivers = $mail_array['receivers'];
		$mails->mail_title = $mail_array['mail_title'];
		$mails->mail_body = $mail_array['mail_body'];
		$mails->creator = $mail_array['creator'];
		if (isset($mail_array['schedule_time']))
			$mails->schedule_time = $mail_array['schedule_time'];
		if (isset($mail_array['remark']))
			$mails->remark = $mail_array['remark'];
		if (isset($mail_array['relative_code']))
			$mails->relative_code = $mail_array['relative_code'];
		if (isset($mail_array['sent_ok'])){
			$mails->sent_ok = $mail_array['sent_ok'];
		}else{
			$mails->sent_ok = 0;
		}
			
		$mails->sent_times = 0;
		
		$dbca->save($mails,'backend');
	}
	
	/**
	 * 2012-03-08
	 * 及时邮件发送方法
	 * @param array $mail_array = array('title'=>'邮件标题','body'=>'邮件内容','custName'=>'邮件称呼','AddAddress'=>'收件人地址')
	 * 
	 */
	public function send_mail($mail_array){
		global $G_SHOP;
		/*$inAddress = self::checkEmail($mail_array["AddAddress"]);
		if(!$inAddress){
			 self::$_currentErrorMessage = '请正确填写邮箱地址';
   		     return -1;
		}
		*/
	    $out_title = $mail_array["title"];
	    $out_body = $mail_array["body"];
	    $out_address = $mail_array["AddAddress"];
	    $out_custName = $mail_array["custName"];
	    
	    $mail_instance =  mailerFactory::create ();
	    
	    $getencoding = mb_detect_encoding($out_title);
		$mail_instance->Subject = iconv($getencoding,G_MAIL_CHARSET . "//IGNORE",$out_title);
		
		$getencoding = mb_detect_encoding($out_body);
		$out_body = iconv($getencoding,G_MAIL_CHARSET . "//IGNORE",$out_body);
		
		$getencoding = mb_detect_encoding($out_address);
		$out_address = iconv($getencoding,G_MAIL_CHARSET . "//IGNORE",$out_address);
		
		$getencoding = mb_detect_encoding($out_custName);
		$out_custName = iconv($getencoding,G_MAIL_CHARSET . "//IGNORE",$out_custName);
		
		$mail_instance->AddAddress($out_address,$out_custName);
		$mail_instance->IsHTML();
    	
		$mail_instance->MsgHTML($out_body);
		if (!$mail_instance->Send()){
			self::$_currentErrorMessage = '发送邮件失败';
			return -1;
		}else{
			//发送邮件成功
			return 2;
		}
	}
}

?>